<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="admin.Connectmysql" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>在线考试 - <%= request.getParameter("exam") %></title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1 { color: #333; }
        .question { margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #eee; }
        .question p { font-weight: bold; }
        .options { list-style: none; padding: 0; }
        .options li { margin-bottom: 8px; }
        .submit-btn { padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; }
        .submit-btn:hover { background-color: #45a049; }
    </style>
</head>
<body>
<h1>考试：<%= request.getParameter("exam") %></h1>

<form action="submitExam.jsp" method="post">
    <input type="hidden" name="exam" value="<%= request.getParameter("exam") %>">

    <%
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String examName = request.getParameter("exam");
        Connectmysql cmysql = new Connectmysql(
                "jdbc:mysql://localhost:3306/exam1",
                "root",
                "root",
                "com.mysql.cj.jdbc.Driver");
        try {
            if(cmysql.open()){
            // 查询指定exam的所有试题
            String sql = "SELECT * FROM question WHERE exam = ? ORDER BY id";
            rs = cmysql.executeSql(sql,new Object[] {examName});
            while (rs.next()) {
                int questionId = rs.getInt("id");
                String questionText = rs.getString("text");
                String optionA = rs.getString("A");
                String optionB = rs.getString("B");
                String optionC = rs.getString("C");
                String optionD = rs.getString("D");
    %>
    <div class="question">
        <p><%= questionId %>. <%= questionText %></p>
        <ul class="options">
            <li><input type="radio" name="answer_<%= questionId %>" value="A"> A. <%= optionA %></li>
            <li><input type="radio" name="answer_<%= questionId %>" value="B"> B. <%= optionB %></li>
            <li><input type="radio" name="answer_<%= questionId %>" value="C"> C. <%= optionC %></li>
            <li><input type="radio" name="answer_<%= questionId %>" value="D"> D. <%= optionD %></li>
        </ul>
        <input type="hidden" name="question_<%= questionId %>" value="<%= questionId %>">
        <input type="hidden" name="score_<%= questionId %>" value="<%= rs.getInt("score") %>">
    </div>
    <%
            }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println("<p>加载试题失败，请稍后重试。</p>");
        } finally {
            // 关闭数据库连接
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    %>

    <input type="submit" value="提交答案" class="submit-btn">
</form>
</body>
</html>